//
//  PhotoCollectionViewController.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/20.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit

private let cellId = "PhotoCell"

class PhotoCollectionViewController: UICollectionViewController {
    // MARK:-属性
    lazy var images: [UIImage] = [UIImage]()
    // MARK:-系统回调方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // 注册cell
        collectionView?.registerNib(UINib(nibName: "PhotoCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellId)
    }
}

// MARK:-数据源方法
extension PhotoCollectionViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! PhotoCollectionViewCell
        
        cell.delegate = self
        cell.image = indexPath.item < images.count ? images[indexPath.item] : nil
        
        return cell
    }
}

// MARK:-cell代理方法
extension PhotoCollectionViewController: PhotoCollectionViewCellDelegate {
    func addPhotoTocollectionViewCell(cell: PhotoCollectionViewCell) {
        //判断照片源是否可用
        if !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
        return
        }
        //弹出照片控制器
        let imagePickerVc = UIImagePickerController()
        imagePickerVc.sourceType = .PhotoLibrary
        imagePickerVc.delegate = self
        presentViewController(imagePickerVc, animated: true, completion: nil)
    }
    
    func deletePhotoFromcollectionViewCell(cell: PhotoCollectionViewCell) {
        //拿到cell对应的位置,从数组删除
        let indexPath = collectionView?.indexPathForCell(cell)
        images.removeAtIndex(indexPath!.item)
        collectionView?.reloadData()
    }
}

// MARK:-UIImagePickerController代理方法
extension PhotoCollectionViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        //将点击的照片放入模型数组
        images.append(image)
        //退出控制器
        picker.dismissViewControllerAnimated(true) { () -> Void in
            //刷新列表
            self.collectionView?.reloadData()
        }
    }
}

// MARK:-自定义流水布局类
class PhotoCollectionViewLayout: UICollectionViewFlowLayout {
    override func prepareLayout() {
        super.prepareLayout()
        // 流水布局属性
        let itemPadding: CGFloat = 15.0
        let itemWH = (UIScreen.mainScreen().bounds.width - 4 * itemPadding) / 3
        itemSize = CGSizeMake(itemWH, itemWH)
        minimumInteritemSpacing = itemPadding
        minimumLineSpacing = itemPadding
        // collectionView属性
        collectionView?.contentInset = UIEdgeInsetsMake(itemPadding, itemPadding, 0, itemPadding)
        collectionView?.backgroundColor = UIColor.grayColor()
    }
}